#include <iostream>
#include <vector>
#include <set>
#include <functional>
#include <string>
#include <algorithm>

using namespace std;

// 自定义仿函数
class Equal{
private:
	string msg;
public:
	Equal(string msg)
	{
		this->msg = msg;
	}
	bool operator () (const string &info)
	{
		return this->msg == info;
	}
};

void print(int number)
{
	cout << "number: " << number << endl;
}

int main()
{

	// 预定义函数对象，和函数适配器
	//set<string,less<string>> st;
	set<string,greater<string>> st;
	
	st.insert("aaa");
	st.insert("bbb");
	st.insert("ccc");
	st.insert("ddd");

	for (set<string>::iterator it = st.begin(); it != st.end(); it++) {
		cout << "name: " << it->c_str() << endl;	
	}
	
	// plus预定义函数进行拼接
	plus<string> pls;
	string str = pls("aaa","mmm");
	cout << str << endl;

	// 函数适配器，bind2nd，将两个函数进行绑定
	set<string,greater<string>>::iterator it = find_if(st.begin(), st.end(), bind2nd(equal_to<string>(), "aaa"));

	if(it != st.end())
	{
		cout << "it is find" << endl;
	}
	else 
	{
		cout << "it not find" << endl;
	}

	int count = count_if(st.begin(), st.end(), Equal("aaa"));
	cout << "count: " << count << endl;

	int count2 = count_if(st.begin(), st.end(), bind2nd(equal_to<string>(), "bbb"));
	cout << "count2: " << count2 << endl;


	/*
	 * 预定义适配器
	 * 1.equal_to
	 * 2.count count_if
	 * 3.merge
	 * 4.sort
	 * 5.ramdom_shuffle
	 * 6.copy
	 * 7.replace
	 * */

	vector<int> vec;
	vec.push_back(11);
	vec.push_back(13);
	vec.push_back(14);
	vec.push_back(11);
	vec.push_back(13);
	vec.push_back(14);
	vec.push_back(15);
	vec.push_back(16);
	vec.push_back(18);

//	int number = count(vec.begin(),vec.end(),11);
//	cout << "11 count: " << number << endl;

	int num_less = count_if(vec.begin(),vec.end(),bind2nd(less<int>(), 14));
	cout << "14 less: " << num_less << endl;

	int num_grea = count_if(vec.begin(),vec.end(),bind2nd(greater<int>(), 14));
	cout << "14 grea: " << num_grea << endl;

	vector<int> vec1;
	vec1.push_back(11);
	vec1.push_back(13);
	vec1.push_back(14);

	vector<int> vec2;
	vec2.push_back(11);
	vec2.push_back(13);
	vec2.push_back(14);

	vector<int> vec3;
	vec3.resize(6);
	merge(vec1.begin(),vec1.end(),vec2.begin(),vec2.end(),vec3.begin());
	cout << "---------merge---------" << endl;
	for (vector<int>::iterator it = vec3.begin(); it != vec3.end(); it++) {
		cout << "meger: " << *it << endl;	
	}
	
	cout << "---------sort---------" << endl;
	sort(vec3.begin(),vec3.end(),greater<int>());
	for_each(vec3.begin(), vec3.end(), print);

	cout << "---------ramdom_shuffle---------" << endl;
	random_shuffle(vec3.begin(),vec3.end());
	for_each(vec3.begin(), vec3.end(), print);
	
	cout << "---------copy---------" << endl;
	vector<int> vec4;
	vec4.resize(2);
	copy(vec3.begin(),vec3.begin() + 2,vec4.begin());
	for_each(vec4.begin(), vec4.end(), print);
	
	cout << "---------replace---------" << endl;
	replace(vec4.begin(),vec4.end(),13,999);
	for_each(vec4.begin(), vec4.end(), print);

	/*
	 * STL设计思想：数据与算法分离
	 * */

	return 0;
}

